package com.basic.common.business;

import com.alibaba.fastjson.JSONObject;
import com.basic.common.area.AreaReqDsBean;
import com.basic.common.area.AreaResDsBean;
import com.basic.common.bean.UrlDsBean;
import com.basic.common.constant.UrlDsConstant;
import com.basic.common.exception.Errors;
import com.basic.common.exception.SystemCodeDsBean;
import com.basic.common.exception.handle.ExceptionReqDsBean;
import com.basic.common.http.HttpClientDsUtil;
import com.basic.common.json.JsonDsUtil;
import com.basic.common.result.InterfaceBean;
import com.basic.common.result.WebResult;
import com.basic.common.spring.SpringContextDsUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

//todo 改造 直接调方法
/**
 * @author
 * @version v_1.0.1
 * @since
 */
public class BasicDsUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(BasicDsUtil.class);

    public static BasicDsService basicDsService = null;

    public static UrlDsBean urlIBean = null;

    public static SystemCodeDsBean selErrorByCode(String code) {
        return selErrorByTypeAndCode(null, code);
    }

    public static SystemCodeDsBean selErrorByTypeAndCode(String type, String code) {
        if (StringUtils.isBlank(code)) {
            return null;
        }
        try {
            basicDsService = (BasicDsService) SpringContextDsUtil.getBean("basicDsService");
            return basicDsService.selErrorByTypeAndCode(type, code);
        } catch (Exception e) {
            LOGGER.info("basicDsService exception: {}", e.getMessage());
            urlIBean = (UrlDsBean) SpringContextDsUtil.getBean("urlIBean");
            String exceptionCodeUrl = urlIBean.getBasicUrl() + UrlDsConstant.BASIC_EXCEPTION_CODE;
            if (StringUtils.isBlank(exceptionCodeUrl)) {
                return null;
            }
            Map<String, String> map = new HashMap<>(16);
            map.put("code", code);
            if (StringUtils.isNotEmpty(type)) {
                map.put("type", type);
            }
            String s1 = HttpClientDsUtil.doPostJsonNotLog(exceptionCodeUrl, map);
            WebResult webResult = JsonDsUtil.jsonStrToBean(s1, WebResult.class);
            if (Errors.SUCCESS.code.equals(webResult.getCode())) {
                Object data = webResult.getData();
                if (data != null) {
                    try {
                        String jsonStr = JSONObject.toJSONString(data);
                        return JsonDsUtil.jsonStrToBean(jsonStr, SystemCodeDsBean.class);
                    } catch (Exception e1) {
                        String jsonStr = JSONObject.toJSONString(data);
                        JSONObject jsonObject = JsonDsUtil.jsonStrToJSONObject(jsonStr);
                        Object value = jsonObject.get("value");
                        Object msg = jsonObject.get("msg");
                        SystemCodeDsBean systemCodeDsBean = new SystemCodeDsBean();
                        systemCodeDsBean.setCode(code);
                        systemCodeDsBean.setValue(value.toString());
                        systemCodeDsBean.setMsg(msg.toString());
                        return systemCodeDsBean;
                    }
                }
            }
        }
        return null;
    }

    public static boolean isRightRequest(InterfaceBean interfaceBean) {
        try {
            basicDsService = (BasicDsService) SpringContextDsUtil.getBean("basicDsService");
            return basicDsService.isRightRequest(interfaceBean);
        } catch (Exception e) {
            urlIBean = (UrlDsBean) SpringContextDsUtil.getBean("urlIBean");
            String isRightRequestUrl = urlIBean.getBasicUrl() + UrlDsConstant.BASIC_IS_RIGHT_REQUEST;
            if (StringUtils.isNotBlank(isRightRequestUrl)) {
                String s = HttpClientDsUtil.doPostJsonNotLog(isRightRequestUrl, JsonDsUtil.beanToJSONStr(interfaceBean));
                try {
                    WebResult webResult = JSONObject.parseObject(s, WebResult.class);
                    return (boolean) webResult.getData();
                } catch (Exception e1) {
                    return false;
                }
            }
        }
        return false;
    }

    public static void add(ExceptionReqDsBean exceptionReqDsBean) {
        try {
            basicDsService = (BasicDsService) SpringContextDsUtil.getBean("basicDsService");
            basicDsService.add(exceptionReqDsBean);
        } catch (Exception e) {
            urlIBean = (UrlDsBean) SpringContextDsUtil.getBean("urlIBean");
            String exceptionAddUrl = urlIBean.getBasicUrl() + UrlDsConstant.BASIC_EXCEPTION_ADD;
//            new SendExceptionDsThread(exceptionAddUrl, JsonDsUtil.beanToJSONStr(exceptionReqDsBean)).start();
        }
    }

    public static AreaResDsBean selAreaByFullName(String addrName) {
        try {
            basicDsService = (BasicDsService) SpringContextDsUtil.getBean("basicDsService");
            AreaResDsBean areaResDsBean = basicDsService.selAreaByFullName(addrName);
            return areaResDsBean;
        } catch (Exception e) {
            urlIBean = (UrlDsBean) SpringContextDsUtil.getBean("urlIBean");
            AreaReqDsBean areaIReqBean = new AreaReqDsBean();
            areaIReqBean.setAreaFullName(addrName);
            String basicUrl = urlIBean.getBasicUrl();
            String s = HttpClientDsUtil.doPostJson(basicUrl + UrlDsConstant.BASIC_AREA_FULLNAME, JsonDsUtil.beanToJSONStr(areaIReqBean));
            WebResult webResult = JsonDsUtil.jsonStrToBean(s, WebResult.class);
            if (webResult != null && Errors.SUCCESS.equals(webResult.getCode())) {
                Object data = webResult.getData();
                if (data != null) {
                    AreaResDsBean areaIResBean = JsonDsUtil.jsonStrToBean(data.toString(), AreaResDsBean.class);
                    return areaIResBean;
                }
            }
        }
        return null;
    }

}
